import ts = require('typescript/lib/typescript.js');

function updateImportNode(node: ts.Node, ctx: ts.TransformationContext) {
  const visitor: ts.Visitor = (node) => {
    let identifierName: string | undefined;

    if (ts.isNamedImports(node)) {
      const identifierName = node.getChildAt(1).getText();
      return ts.factory.createIdentifier(identifierName);
    }

    if (ts.isStringLiteral(node)) {
      // import moduleDeclaration 显然不会出现 \`
      const libName = node.getText().replace(/[\"\']/g, '');
      if (identifierName) {
        const fileName = camel2Dash(identifierName);
        return ts.factory.createStringLiteral(`${libName}/lib/${fileName}`);
      }
    }

    if (node.getChildCount()) {
      return ts.visitEachChild(node, visitor, ctx);
    }
    return node;
  };
}
function camel2Dash(str: string) {
  return str
    .replace(/([A-Z])/g, '-$1')
    .toLowerCase()
    .slice(1);
}
// 以上，我们就成功实现了如下代码转换：

// before
// import { Button } from 'antd'

// after
// import Button from 'antd/lib/button'
let a!: unknown;

if (a instanceof Error) {
  a;
  //^?
}
